package net.scrumtool.controller;

import net.scrumtool.model.Collaborator;
import net.scrumtool.technics.DAO;
import net.scrumtool.technics.Utils;
import net.scrumtool.view.Profile;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Label;

public class Cprofile {

	Profile view;

	private Collaborator collaboratorActif;

	public Cprofile(Profile view) {
		this.view = view;
	}

	public void init(Collaborator collaborator) {
		view.getShell().setSize(381, 211);
		view.getTxtFirstnameProfile().setText(collaborator.getFirstname());
		view.getTxtLastnameProfile().setText(collaborator.getLastname());
		view.getTxtEmailProfile().setText(collaborator.getEmail());
	}

	public void listener(final Collaborator collaborator, final Label lblInfosUser) {

		view.getBtnEditProfile().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Boolean email = false;
				java.util.List<Collaborator> verifEmail = DAO.getCollaborators();
				for (Collaborator emailV : verifEmail) {
					if (emailV.getEmail().equals(view.getTxtEmailProfile().getText())) {
						if (emailV.getEmail().equals(collaborator.getEmail())) {
							email = false;
						} else {
							email = true;
						}
					}
				}

				if (email == true) {
					Utils.MsgBox(view.getShell(), "Email already exist", "error");
				} else {
					if (view.getBtnChangePasswordProfile().getSelection()) {
						if (view.getTxtCurrentPasswordProfile().getText().equals(collaborator.getPassword())) {
							if (view.getTxtNewPasswordProfile().getText().equals(view.getTxtConfirmPasswordProfile().getText())) {
								collaborator.setFirstname(view.getTxtFirstnameProfile().getText());
								collaborator.setLastname(view.getTxtLastnameProfile().getText());
								collaborator.setEmail(view.getTxtEmailProfile().getText());
								collaborator.setPassword(view.getTxtConfirmPasswordProfile().getText());
								DAO.SQLCollaborator(collaborator, "update");
								Utils.MsgBox(view.getShell(), "Profile updated successfully", "information");
								lblInfosUser.setText("Hello " + collaborator.getFirstname() + " " + collaborator.getLastname() + "");
								view.close();
							} else {
								Utils.MsgBox(view.getShell(), "Passwords are not the same", "error");
							}
						} else {
							Utils.MsgBox(view.getShell(), "This is not your current password", "error");
						}
					} else {
						collaborator.setFirstname(view.getTxtFirstnameProfile().getText());
						collaborator.setLastname(view.getTxtLastnameProfile().getText());
						collaborator.setEmail(view.getTxtEmailProfile().getText());
						DAO.SQLCollaborator(collaborator, "update");
						Utils.MsgBox(view.getShell(), "Profile updated successfully", "information");
						lblInfosUser.setText("Hello " + collaborator.getFirstname() + " " + collaborator.getLastname() + "");
						view.close();

					}
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getBtnChangePasswordProfile().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (view.getBtnChangePasswordProfile().getSelection()) {
					view.getShell().setSize(381, 367);
					view.getGroupChangePasswordProfile().setVisible(true);
				} else {
					view.getShell().setSize(381, 211);
					view.getGroupChangePasswordProfile().setVisible(false);
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

	}
}
